// pages/cardInfo/cardInfo.js
var ports = require("../../utils/ports.js")
// 获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sqcheck: true,
    myCardInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCardList(options.cardid)
  },
  getCardList(cardid) {
    let that = this
    ports.ModuleMemberCard.getCardDefineDetail({
      cardDefineID:cardid
    }, that, res => {
      that.setData({
        myCardInfo: res.data.body.data
      })
    })
  },
  cancleCheck() {
    var that = this
    that.setData({
      sqcheck: !that.data.sqcheck
    })
  },
  topay(e) {
    var that = this
    var cardid = e.currentTarget.dataset.id
    if (!that.data.sqcheck) {
      that.setData({
        sqcheck: true
      })
    }
    ports.ModuleMemberCard.buyMemberCard({
      cardDefineID: cardid
    }, that, res => {
      if (res.data.header.code == 0) {
        let payData = {}
        payData.memberPaymentID = res.data.body.memberCardID;
        payData.openid = wx.getStorageSync("USER_OPENID")
        payData.payWayID = app.paywayID
        payData.body = '购买会员卡'
        wx.showToast({
          title: '下单成功，支付暂未开放',
          icon: 'none'
        })
        //that.appApplyMemberOrderPay(payData)
      } else {
        wx.showToast({
          title: res.data.header.msg,
          icon: 'none'
        })
      }
    })
  },
  //申请订单支付
  appApplyMemberOrderPay(data) {
    var that = this
    ports.ModuleMemberOrder.appApplyMemberOrderPay(data, that, res => {
      if (res.data.header.code == 0) {
        var data = res.data.body;
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: data.signType,
          paySign: data.paySign,
          success(res) {
            wx.showToast({
              title: '支付成功',
              icon: 'none',
              mask: true,
              duration: 1000
            })
          },
          fail() {
            wx.showToast({
              title: '支付失败',
              icon: 'none',
              mask: true,
              duration: 1000
            })
          }
        })
      } else {
        wx.showToast({
          title: res.data.header.msg,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})